<template>
  <Container class="storesign-edit">
    <!-- 基础设置 -->
    <div class="container-header title title-center" slot="header">
      查看签约门店
      <Button
        size="large"
        class="fl btn-back"
        icon="ios-arrow-back"
        @click="jumpPrevPage"
      >返回</Button>
    </div>
    <Tabs v-model="tab" type="card" size="small">
      <TabPane :label="renderLabel('基础信息')" name="tab0" />
      <TabPane :label="renderLabel('签约信息', isInfoChange)" name="tab1" />
      <TabPane :label="renderLabel('门店照片', storePicChange)" name="tab2" />
      <TabPane :label="renderLabel('门店证件', idPicChange)" name="tab3"/>
      <TabPane :label="renderLabel('签约记录')" name="tab4" />
      <TabPane :label="renderLabel('扫码地址')" name="tab5" />
      <TabPane :label="renderLabel('审核进度')" name="tab6" />
    </Tabs>
    <div class="tab-content">
      <StoreBaseInfoItem :showJudge='true' :form="form" v-show="tab=='tab0'"  />
      <StoreInfoItem :showJudge='true' :form="form" v-show="tab=='tab1'" :isOpen="isOpen" />
      <StorePictures :form="form" v-show="tab=='tab2'"  />
      <IDcardPictures :isEdit='true' :isExit="isExit" :form="form" :id="form.storeDTO.id" v-show="tab=='tab3'" @checkNo="onCheckNoblur" />
      <SignRecordItem :form="form" v-if="tab=='tab4'"  />
      <AddressMap :contractId="$route.query.contractId" :form="form" :isFormStoreCheck="true" v-if="tab=='tab5'"  />
      <ProgressItem :form="form" :applyId="applyId" v-if="tab=='tab6'"  />
    </div>
    
    <div class="container-footer bg-white" slot="footer" v-if="auditStatus === 0 || auditStatus === 1 ">
      <Input v-if="$route.name === 'manage/storecheck/edit'" type="textarea" placeholder="请输入处理说明" v-model="auditRemark" />
      <Button v-if="$route.name === 'manage/storecheck/edit'" :loading='rejectBtnloading' type="primary" size="large" class="ml" @click="onReject">驳回</Button>
      <Button v-if="$route.name === 'manage/storecheck/edit'" :disabled="isExit" :loading='passBtnloading' type="primary" size="large" class="ml" @click="getCheck">审核通过</Button>
      <!-- <Button v-if="$route.name === 'manage/storecheck/edit'" :disabled="isExit" :loading='passBtnloading' type="primary" size="large" class="ml" @click="onPass">审核通过</Button> -->
      <Button type="primary" size="large" class="ml" @click="jumpPrevPage">取消</Button>
    </div>
    <Loader :status="status" @refresh="getData"></Loader>
    <EditItem ref="editItem" @complete="getData"></EditItem>
  </Container>
</template>
<script>
  import StoreInfoItem from "./edit/DefaultInfoItem";
  import StoreBaseInfoItem from "../storesign/edit/StoreBaseInfoItem";
  import StorePictures from '../storesign/edit/StorePicturesItem'
  import SignRecordItem from '../storesign/edit/SignRecordItem'
  import AddressMap from '../storesign/edit/AddressMapItem'
  import IDcardPictures from '../storesign/edit/IDcardPicturesItem'
  import ProgressItem from '../storesign/edit/ProgressItem'
  import EditItem from './edit/EditItem'

  export default {
    name: "",
    components: {
      StoreBaseInfoItem,
      StoreInfoItem,
      StorePictures,
      SignRecordItem,
      AddressMap,
      IDcardPictures,
      ProgressItem,
      EditItem
    },
    data() {
      return {
        tab: 'tab1',
        form: {
          cardz: [],
          cardf: [],
          yyzz: [],
          storeContractApplyDTO: {},
          storeContractAgreementDTOList: [],
          storeContractChangeLogDTOList: [],
          empowerBrandDTOList: [],
          storeContractDTO: {
            distributorId: '',
            distributorName: '',
            storeContractBrandDTOList: [],
            storeContractAgreementDTO: {},
            storeContractExtDTO: {}
          },
          storeDTO: {},
          id: '',
        },
        renderLabel: (text, changed) => {
          return (h)=> {
            return <span class={changed?'font-pink':''}>{text}</span>
          }
        },
        cur: {},
        pre: {},
        auditRemark: '',
        auditStatus: 3, // 待审核 审核中  审核不通过 审核通过
        id: '',
        status: 'pass',
        rejectBtnloading: false,
        passBtnloading: false,
        isExit: false,
        isOpen: null
      }
    },
    computed: {
      isInfoChange() {
        let ishave = false
        let info = this.form.storeContractDTO
        let val = [
          'storeNameOrg', 'distributorNameOrg', 'storeTypeNameOrg', 'storeTaskQuantityOrg', 'storeNoOrg', 'contractStatusStrOrg', 'salesmanNameOrg', 
          'ownerBirthdayOrg', 'storePhoneOrg', 'maxRebateQuantityOrg', 'maxPurchaseQuantityIntegralOrg', 'maxFundQuantityOrg', 'inRebateExpiredDaysOrg',
          'scanDistanceRangeOrg', 'unitFundOrg'
        ]
        let bv = [
          'isOpenFundOrg', 'isOpenInspectionOrg', 'isOpenProtectionClaimsOrg', 'isOpenCreditAccountOrg'
        ]
        if (val.find(item => info[item])) {
          ishave = true
        }
        if (bv.find(item => (info[item] === true || info[item] === false))) {
          ishave = true
        }
        return ishave
      },
      storePicChange() {
        let imgs = this.form.storeContractDTO.storeContractExtDTO
        return imgs.headImageUrlOrg || imgs.shelfImageUrlOrg || imgs.hangerImageUrlOrg || imgs.stationImageUrlOrg || imgs.posterImageUrlOrg
      },
      idPicChange() {
        let imgs = this.form.storeContractDTO.storeContractExtDTO
        return imgs.ownerIdcardPositiveImageUrlOrg || imgs.ownerIdcardNegativeImageUrlOrg || imgs.businessLicenseImageUrlOrg
      }
    },
    mounted() {
      this.id = this.$route.query.id
      this.getData()
    },
    methods: {
      getData() {
        if (!this.id) return
        this.status = 'loading'
        this.$ajaxGet(`/store_contract_audit/audit/detail/${this.id}`).then(res => {
          this.status = 'pass'
          this.pre = res.storeContractDTO
          this.cur = res.storeContractApplyDTO.applyDetailDTO
          this.applyId = res.storeContractApplyDTO.id
          this.auditStatus = res.storeContractApplyDTO.auditStatus
          if (!this.cur.storeContractExtDTO) this.cur.storeContractExtDTO = {}
          this.compareValue(this.cur, this.pre)
          this.form = Object.assign(this.form, { storeContractDTO: this.cur, storeDTO: res.storeDTO || {} })
          if (!this.form.storeContractDTO.storeContractExtDTO) {
            this.form.storeContractDTO.storeContractExtDTO = {}
          }
          if (!this.form.storeContractDTO.storeContractAgreementDTO) {
            this.form.storeContractDTO.storeContractAgreementDTO = {}
          }
          if ((this.auditStatus === 0 || this.auditStatus === 1) && this.$route.name === 'manage/storecheck/edit') this.onCheckNo()
        }).catch((err) => {
          this.status = 'error'
        })
      },
      onCheckNo () {
        let param = {
          businessLicenseNo: this.form.storeContractDTO.businessLicenseNo,
          storeId: this.form.storeDTO.id
        }
        this.$ajaxPost('/store_contract_audit/audit/check_businessno', param).then(res => {
          this.isExit = res
        })
      },
      onCheckNoblur (val) {
        this.isExit = val
      },
      onReject() {
        let auditRemark = this.auditRemark
        if(!auditRemark) {
          this.$Message.warning('请输入驳回原因')
          return
        }
        let params = {
          auditRemark,
          applyId: this.id,
          isPass: false,
        }
        this.rejectBtnloading = true
        this.$jsonPost('/store_contract_audit/audit/commit', params).then(res => {
          this.rejectBtnloading = false
          this.$Message.info('操作成功')
          this.jumpPrevPage()
        }).catch(() => {
          this.rejectBtnloading = false
        })
      },
      getCheck () {
        let params = {
          applyId: this.id
        }
        this.$ajaxPost('/store_contract_audit/audit/check_store_type_enable', params).then(res => {
          this.isOpen = res
          let auditRemark = this.auditRemark
          let {
            ownerName, storeName, storeTaskQuantity, storeTypeId, storeTypeName,
            ownerIdcardNo, businessLicenseNo, ownerIdcardBeginDate, ownerIdcardEndDate
          } = this.form.storeContractDTO
          let param = {
            auditRemark,
            applyId: this.id,
            isPass: true,
            ownerName,
            storeName,
            storeTaskQuantity,
            storeTypeId,
            storeTypeName,
            ownerIdcardNo,
            businessLicenseNo,
            ownerIdcardBeginDate,
            ownerIdcardEndDate
          }
          if (this.isOpen) {
            this.$refs.editItem.updateForm(param)
          } else {
            if (!storeName) {
              this.$Notice.warning({ title: '请输入门店名称' })
              return
            }
            if (!ownerName) {
              this.$Notice.warning({ title: '请输入门店联系人' })
              return
            }
            if (!storeTypeId) {
              this.$Notice.warning({ title: '请选择门店类型' })
              return
            }
            this.passBtnloading = true
            this.$jsonPost('/store_contract_audit/audit/commit', param).then(res => {
              this.$Message.info('操作成功')
              this.jumpPrevPage()
              this.passBtnloading = false
            }).catch(() => {
              this.passBtnloading = false
            })
          }
        })
      },
      onPass() {
        let auditRemark = this.auditRemark
        let {
          ownerName, storeName, storeTaskQuantity, storeTypeId, storeTypeName,
          ownerIdcardNo, businessLicenseNo, ownerIdcardBeginDate, ownerIdcardEndDate
        } = this.form.storeContractDTO
        if (!storeName) {
          this.$Notice.warning({ title: '请输入门店名称' })
          return
        }
        if (!ownerName) {
          this.$Notice.warning({ title: '请输入门店联系人' })
          return
        }
        if (!storeTypeId) {
          this.$Notice.warning({ title: '请选择门店类型' })
          return
        }
        let params = {
          auditRemark,
          applyId: this.id,
          isPass: true,
          ownerName,
          storeName,
          storeTaskQuantity,
          storeTypeId,
          storeTypeName,
          ownerIdcardNo,
          businessLicenseNo,
          ownerIdcardBeginDate,
          ownerIdcardEndDate
        }
        this.passBtnloading = true
        this.$jsonPost('/store_contract_audit/audit/commit', params).then(res => {
          this.$Message.info('操作成功')
          this.jumpPrevPage()
          this.passBtnloading = false
        }).catch(() => {
          this.passBtnloading = false
        })
      },
      compareValue(cur, pre) {
        if (!pre) return
        for (let i in cur) {
          if (cur[i] && Object.prototype.toString.call(cur[i]) != "[object Object]" && cur[i] != pre[i]) {
            cur[`${i}Org`] = pre[i]
          }
        }
        this.compareValue(cur.storeContractExtDTO, pre.storeContractExtDTO)
      },
      jumpPrevPage() {
        this.$router.back()
      },
    }
  };
</script>
